package com.ukao.pad.ui.myData;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.ukao.pad.R;
import com.ukao.pad.adapter.MyDataClothingAdapter;
import com.ukao.pad.base.BaseFragment;
import com.ukao.pad.bean.StringBean;
import com.ukao.pad.consts.Constant;
import com.ukao.pad.consts.SaveParamets;
import com.ukao.pad.retrofit.ApiCallback;
import com.ukao.pad.ui.leaveFactory.MyMarkerView;
import com.ukao.pad.utils.DateUtils;
import com.ukao.pad.utils.Utils;
import com.ukao.pad.widget.DividerDecoration;
import com.ukao.pad.widget.XValueFormatter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MyDataFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.chart)
    LineChart chart;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    Unbinder unbinder;

    @BindView(R.id.line_chart_title)
    TextView lineChartTitle;


    private String type;
    private String mParam2;


    public static MyDataFragment newInstance(String param1, String param2) {
        MyDataFragment fragment = new MyDataFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_data, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }
    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        String startTime="";  //year,month, day
        String typeData="";
        long currentTime = System.currentTimeMillis();
        String endTime = DateUtils.formatDataTime(currentTime);
        Calendar now = Calendar.getInstance();
        switch (type) {
            case "1":
                now.add(Calendar.DAY_OF_MONTH, -1);
                 startTime = DateUtils.formatDataTime(now.getTime());
                typeData="day";
                break;
            case "2":
                startTime=endTime;
                typeData="day";
                break;
            case "3":
                now.add(Calendar.DAY_OF_MONTH, -7);
                startTime = DateUtils.formatDataTime(now.getTime());
                typeData="day";
                break;
            case "4":
                now.add(Calendar.MONTH, -1);
                startTime = DateUtils.formatDataTime(now.getTime());
                typeData="day";
                break;
            case "5":
                now.add(Calendar.MONTH, -3);
                startTime = DateUtils.formatDataTime(now.getTime());
                typeData="day";
                break;
            case "6":
                now.add(Calendar.YEAR, -1);
                startTime = DateUtils.formatDataTime(now.getTime());
                typeData="month";
                break;

        }
        //myData(typeData,startTime,endTime);



    }

    public void myData(String  type,String    startDate,String    endDate ) {
        showProgressDialog();
        HashMap mHashMap=new HashMap<String,String>();  //type（year,month, day）
        mHashMap.put("type",type);
        mHashMap.put("startDate",startDate);
        mHashMap.put("endDate",endDate);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores().myData(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {


                        }else{


                        }
                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {dismissProgressDialog();
                    }
                });
    }


    @Override
    protected void initView() {

        List<String> mData = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            mData.add("");
        }

        Calendar now = Calendar.getInstance();
        //now.add(Calendar.DAY_OF_MONTH, -30);
        //  String startThirtiTime = simpleDateFormat.format(now.getTime());
        showChart(chart);
        initLinearRecyclerView(recyclerView);
        recyclerView.addItemDecoration(new DividerDecoration(getActivity(), DividerDecoration.VERTICAL_LIST));
        MyDataClothingAdapter madapter = new MyDataClothingAdapter(_mActivity, mData);
        recyclerView.setAdapter(madapter);

    }


    private void showChart(LineChart lineChart) {
        setChartEnabled(lineChart);


        lineChart.setBackgroundResource(R.drawable.mydata_bg); //设置背景颜色
        showPopWindom(lineChart);
        Legend mLegend = lineChart.getLegend(); //设置标示，就是那个一组y的value的
        mLegend.setForm(Legend.LegendForm.CIRCLE); //样式
        mLegend.setFormSize(6f); //字体
        mLegend.setTextColor(Color.WHITE); //颜色
        XAxis xAxis = lineChart.getXAxis();  //x轴的标示
        xAxis.setAxisLineColor(Color.parseColor("#3B5B83"));
        xAxis.setAxisLineWidth(3f);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); //x轴位置
        xAxis.setTextColor(Color.WHITE);    //字体的颜色
        xAxis.setTextSize(14f); //字体大小
        xAxis.setDrawGridLines(false); //不显示网格线
        LimitLine ll1 = new LimitLine(100f, "");
        ll1.setLineWidth(0.5f);
        ll1.setLineColor(Color.parseColor("#4D6481"));
        ll1.enableDashedLine(2f, 5f, 0f);
        ll1.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_TOP);
        xAxis.setValueFormatter(new XValueFormatter());
        YAxis axisLeft = lineChart.getAxisLeft(); //y轴左边标示
        YAxis axisRight = lineChart.getAxisRight(); //y轴右边标示
        axisLeft.setZeroLineColor(Color.TRANSPARENT);
        axisLeft.setTextColor(Color.TRANSPARENT);
        axisLeft.setAxisLineColor(Color.TRANSPARENT);
        axisLeft.addLimitLine(ll1);
        axisLeft.setSpaceBottom(5f);
        axisLeft.setAxisMaximum(200f);
        axisLeft.setAxisMinimum(-50f);
        axisLeft.setDrawGridLines(false);
        axisLeft.setDrawZeroLine(true);
        axisLeft.setDrawLimitLinesBehindData(true);
        axisRight.setEnabled(false);
        axisLeft.setSpaceTop(20f);//轴上最高位置在表中最高位置的顶部间距，占总轴的百分比
        axisRight.setDrawAxisLine(false);
        axisRight.setDrawGridLines(false);
        axisRight.setDrawLabels(false);
        chart.animateY(3000);
        lineChart.animateX(2500);  //立即执行动画

        setData(100); //填充数据
    }

    private void showPopWindom(LineChart lineChart) {
        MyMarkerView mv = new MyMarkerView(_mActivity, R.layout.custom_marker_view);
        mv.setChartView(lineChart);
        lineChart.setMarker(mv);
    }

    /**
     * 禁止拖拽
     */
    private void setChartEnabled(LineChart lineChart) {
        lineChart.setDrawBorders(false); //在折线图上添加边框
        lineChart.setNoDataText("暂无数据");
        lineChart.setDrawGridBackground(false); //表格颜色
        lineChart.setTouchEnabled(true); //可点击
        lineChart.setDragEnabled(false);  //可拖拽
        lineChart.setScaleEnabled(false);  //可缩放
        lineChart.setPinchZoom(false);
        lineChart.getDescription().setEnabled(false);
    }


    private void setData(float range) {
        ArrayList<Entry> values = new ArrayList<Entry>();

        for (int i = 0; i < 7; i++) {
            float val = 0f;
            if (i == 0) {
                val = 100f;
            } else {
                val = (float) (Math.random() * range) + 3;
            }

            values.add(new Entry(1502615052+(i*1000), val, null, "18568件"));
        }
        LineDataSet set1;
        if (chart.getData() != null &&
                chart.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) chart.getData().getDataSetByIndex(0);
            set1.setValues(values);
            chart.getData().notifyDataChanged();
            chart.notifyDataSetChanged();
        } else {


            set1 = new LineDataSet(values, "");
            set1.setHighLightColor(Color.TRANSPARENT);
            set1.setColor(resources.getColor(R.color.blue));// 显示颜色
            set1.setCircleColor(resources.getColor(R.color.blue)); // 圆形的颜色
            set1.setLineWidth(2f);// 线宽
            set1.setCircleRadius(5f);// 显示的圆形大小
            set1.setDrawValues(false);  //设置是否显示每个点的值
            ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
            dataSets.add(set1);

            LineData data = new LineData(dataSets);

            // set data
            chart.setData(data);
        }


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
